/***************************************************************************
 *   Copyright (C) 2008 by Luca Lanziani   *
 *   luca@tulug.it   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "util.h"
#define LISTEN_ERROR -4
#define SETSOCKOPT_ERROR -2
#define BIND_ERROR -3
#define CREATE_SOCKET_ERROR -1

int inizialize_socket(struct sockaddr_in* server_socket, int port) {
	int socket_descriptor;
	int optval;
	
	server_socket->sin_family=AF_INET;
	server_socket->sin_port=htons(port);
	server_socket->sin_addr.s_addr=INADDR_ANY;
	memset(server_socket->sin_zero,'\0',8);
	
	if ((socket_descriptor=socket(AF_INET,SOCK_STREAM,0))<0) {
		printf(_("Error on create socket"));
		return CREATE_SOCKET_ERROR;
	}
	if (setsockopt(socket_descriptor,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof(optval))<0) {
		printf(_("Error on setsockopt"));
		return SETSOCKOPT_ERROR;
	}
	if (bind(socket_descriptor,(struct sockaddr *) server_socket,sizeof(*server_socket))<0) {
		printf(_("Error on bind"));
		return BIND_ERROR;
	}
	if (listen(socket_descriptor,MAXCONNECTION)<0)
	{
		printf(_("Error on listen"));
		return LISTEN_ERROR;
	}
	printf("socket_descriptor %d",socket_descriptor);
	return socket_descriptor;
}